package com.haimi.shiguang.business.user.register;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.haimi.shiguang.R;
import com.haimi.shiguang.api.ApiServeice;
import com.haimi.shiguang.base.BaseFragment;
import com.haimi.shiguang.base.biz.BaseResponse;
import com.haimi.shiguang.base.biz.ResultStatus;
import com.haimi.shiguang.base.dialog.ProgressManager;
import com.haimi.shiguang.utils.ButtonClickTimer;
import com.haimi.shiguang.utils.TitleCallBack;
import com.haimi.shiguang.utils.TitleView;
import com.haimi.shiguang.utils.ToastUtil;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * @author changsunhaipeng
 * @date 2018/6/17
 */

public class RegisterFragment extends BaseFragment {
    public static final String TAG = RegisterFragment.class.getSimpleName();
    private EditText mPhone;
    private EditText mCode;
    private EditText mPassWord;
    private Button mRegister;
    private Button mGetCode;
    private TextView mUserRule;
    private TitleView mTitle;

    public static RegisterFragment newInstance() {
        Bundle args = new Bundle();
        RegisterFragment fragment = new RegisterFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_register, container, false);
    }

    @Override
    public void initView() {
        mPhone = rootView.findViewById(R.id.et_phone);
        mCode = rootView.findViewById(R.id.et_code);
        mPassWord = rootView.findViewById(R.id.et_password);
        mRegister = rootView.findViewById(R.id.btn_register);
        mGetCode = rootView.findViewById(R.id.btn_get_code);
        mUserRule = rootView.findViewById(R.id.tv_user_rule);
        mTitle = rootView.findViewById(R.id.title_view);
        mTitle.setLeft("", true, R.drawable.ic_return);
        mTitle.setMiddle("注册");
        mRegister.setOnClickListener(this);
        mUserRule.setOnClickListener(this);
        mGetCode.setOnClickListener(this);
    }

    @Override
    public void initListener() {
        mTitle.setTitleCallBack(new TitleCallBack() {
            @Override
            public void callBackLeft() {
                dismissSelf();
            }

            @Override
            public void callBackRight() {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        if (!ButtonClickTimer.canClick(v)) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_user_rule:
                break;
            case R.id.btn_register:
                String phone = mPhone.getText().toString().trim();
                String code = mCode.getText().toString().trim();
                String password = mPassWord.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showToast("验证码不能为空");
                    return;
                }
                register();
                break;
            case R.id.btn_get_code:
                sendCode();
                break;
            default:
                break;
        }
    }

    /**
     * 发送验证码
     */
    private void sendCode() {

    }

    /**
     * 注册
     */
    private void register() {
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = mPhone.getText().toString().trim();
                String code = mCode.getText().toString().trim();
                String password = mPassWord.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showToast("验证码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    ToastUtil.showToast("密码不能为空");
                    return;
                }
                ProgressManager.showProgress(RegisterFragment.this);
                Disposable disposable = ApiServeice.getInstance().register(phone, password, code)
                        .subscribe(new Consumer<BaseResponse>() {
                            @Override
                            public void accept(BaseResponse baseResponse) throws Exception {
                                ProgressManager.closeProgress(RegisterFragment.this);
                                if (baseResponse.status.equals(ResultStatus.success)) {
                                    ToastUtil.showToast("注册成功");
                                } else {
                                    ToastUtil.showToast(baseResponse.message);
                                }
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                ProgressManager.closeProgress(RegisterFragment.this);
                                ToastUtil.showToast("注册失败");
                            }
                        });
                compositeDisposable.add(disposable);
            }
        });
    }
}
